#!/usr/bin/env python3
"""
批量替换Java文件中的javax导入为jakarta导入
用于Spring Boot 2.x升级到3.x的迁移
"""
import os
import re
import sys

def replace_javax_imports(file_path):
    """替换文件中的javax导入为jakarta导入"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        original_content = content
        
        # 替换规则
        replacements = [
            # Servlet API
            (r'import javax\.servlet\.', 'import jakarta.servlet.'),
            
            # Annotation API  
            (r'import javax\.annotation\.', 'import jakarta.annotation.'),
            
            # Validation API
            (r'import javax\.validation\.', 'import jakarta.validation.'),
            
            # Persistence API (JPA)
            (r'import javax\.persistence\.', 'import jakarta.persistence.'),
            
            # Transaction API
            (r'import javax\.transaction\.', 'import jakarta.transaction.'),
            
            # Mail API
            (r'import javax\.mail\.', 'import jakarta.mail.'),
            
            # WebSocket API
            (r'import javax\.websocket\.', 'import jakarta.websocket.'),
            
            # JSON API
            (r'import javax\.json\.', 'import jakarta.json.'),
            
            # XML API
            (r'import javax\.xml\.ws\.', 'import jakarta.xml.ws.'),
            (r'import javax\.xml\.bind\.', 'import jakarta.xml.bind.'),
        ]
        
        # 执行替换
        for pattern, replacement in replacements:
            content = re.sub(pattern, replacement, content)
        
        # 如果有更改，写回文件
        if content != original_content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"✓ 更新: {file_path}")
            return True
        else:
            return False
            
    except Exception as e:
        print(f"✗ 错误处理文件 {file_path}: {e}")
        return False

def process_directory(directory):
    """处理目录中的所有Java文件"""
    updated_files = 0
    total_files = 0
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.java'):
                file_path = os.path.join(root, file)
                total_files += 1
                if replace_javax_imports(file_path):
                    updated_files += 1
    
    return updated_files, total_files

def main():
    """主函数"""
    # 检查src/main/java目录是否存在
    java_dir = "src/main/java"
    if not os.path.exists(java_dir):
        print(f"错误: 目录 {java_dir} 不存在")
        sys.exit(1)
    
    print("开始替换javax导入为jakarta导入...")
    print(f"处理目录: {java_dir}")
    print("-" * 50)
    
    updated_files, total_files = process_directory(java_dir)
    
    print("-" * 50)
    print(f"处理完成!")
    print(f"总文件数: {total_files}")
    print(f"更新文件数: {updated_files}")
    
    if updated_files > 0:
        print("\n重要提醒:")
        print("1. 请检查更新后的文件是否正确")
        print("2. 某些导入可能需要手动调整")
        print("3. 建议运行编译测试确保无错误")

if __name__ == "__main__":
    main() 